package com.rljk.controller.app;/**
 * Created by mac on 2018/7/12.
 */

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.CouponManager;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CouponInController
 * @Description
 * @Author zhang
 * @Date 2018/7/12 16:43
 */
@RestController
@RequestMapping("/coupon")
@Slf4j
public class CouponInController extends BaseController {

    @Resource(name="couponService")
    private CouponManager couponService;

    @Autowired
    RedisService redisService;

    /**
     * @Author zhang
     * @Description  优惠券列表
     * @Date 2018/7/12 17:22
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/listAll")
    public Object couponList() {
        log.info("优惠券列表");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("LISTALL", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("couponList",pd)){

//                    //1.获取用户信息
//                    String token = redisService.get(pd.getString("TOKEN"));
//                    System.out.println(token);
//                    if(Tools.isEmpty(token)){
//                        return Result.error(CodeMsg.TOKEN_ERROR);
//                    }

                     pd.put("COUPON_TYPE",pd.get("COUPON_TYPE"));

                    List<PageData> couponList = couponService.listAll(pd);
                    System.out.println(couponList.size());
                    Iterator<PageData> it = couponList.iterator();
                    while (it.hasNext()){
                        PageData couponInfo = it.next();
                        //总数量
                        Integer totalcount = (Integer) couponInfo.get("TOTALCOUNT");
                        couponInfo.remove("TOTALCOUNT");
                        //卡券被领取数量
                        Integer receivecount = (Integer) couponInfo.get("RECEIVECOUNT");
                        couponInfo.remove("RECEIVECOUNT");
                        couponInfo.remove("USEDCOUNT");
                        couponInfo.remove("COUPON_TYPE");

                        int i = totalcount - receivecount;

                        if (i > 0){
                            couponInfo.put("STATUS",1);
                        }else {
                            couponInfo.put("STATUS",2);
                        }


                    }
                    return Result.success(couponList);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

}
